package com.langxin.site.biz;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.langxin.site.dao.NewsDao;
import com.langxin.site.model.NewsInfo;
import com.langxin.site.model.PageInfo;
import com.langxin.site.model.vo.QueryVo;
import com.langxin.site.util.constant.SiteConstant;

/**
 * 资讯biz
 * @author guqj
 *
 */
@Service
public class NewsBiz extends BaseBiz{

	@Autowired
	private NewsDao  newsDao;
	
	/**
	 * 条件查询
	 * @param baseVo   参数
	 * @param pageSize 每页记录数
	 * @return
	 */
	public List<NewsInfo> queryList(QueryVo queryVo, Integer pageSize, String orderBy){
		if(queryVo == null){
			queryVo = new QueryVo();
		}
		if(pageSize != null){
			queryVo.setPageSize(pageSize);
		}
		if(StringUtils.isNotBlank(orderBy)){
			queryVo.setOrderBy(orderBy);
		}
		return newsDao.query(queryVo);
	}
	
	/**
	 * 总数
	 * @param queryVo
	 * @return
	 */
	public Integer queryNum(QueryVo queryVo){
		return newsDao.queryNum(queryVo);
	}
	
	/**
	 * 分页
	 * @param queryVo
	 * @param pageInfo
	 */
	public void queryPage(QueryVo queryVo, PageInfo<NewsInfo> pageInfo){
		pageInfo.setRecords(queryList(queryVo, 16, null));
		pageInfo.setTotalCount(queryNum(queryVo));
		pageInfo.fill(queryVo);
	}
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	public NewsInfo queryById(Integer id){
		if (!isRead(SiteConstant.CookieKey.NEWS.getValue(), id)) {
			newsDao.updateReadTime(id);
		}
		return newsDao.queryById(id);
	}
	
	/**
	 * 热榜
	 * @param pageSize
	 * @return
	 */
	public List<NewsInfo> hot(Integer pageSize){
		return queryList(null, pageSize, "T." + SiteConstant.ORDER_READ_TIME + " " + SiteConstant.DESC);
	}
}
